import org.acegisecurity.context.SecurityContextHolder as SCH

class SessionTagLib {
    static namespace = "sess"

    def ifLoggedIn = {attrs, body ->
        def authPrincipal = SCH?.context?.authentication?.principal
        if (authPrincipal != null && authPrincipal != "anonymousUser") {
            out << body
        }
    }
    
    def fullName = {attrs, body ->
        def authPrincipal = SCH?.context?.authentication?.principal
        if (authPrincipal != null && authPrincipal != "anonymousUser") {
            out << authPrincipal?.domainClass?.userRealName
        } else {
            out << g.message(code: 'SessionTagLib.fullName.guest')
        }
    }

    def loginLink = {attrs, body ->
        if (session.user == null) {
            out << "<a href=\"" + g.createLinkTo(dir: "", file: "login") + "\">" +
                g.message(code: 'SessionTagLib.fullName.login') + "</a>"
        }
        else {
            out << g.message(code: 'SessionTagLib.fullName.logout')
        }
    }

    def profileLink = {attrs, body ->
        if (session.user == null) {
            out << g.message(code: 'SessionTagLib.fullName.notLoggedIn')
        }
        else {
            out << g.message(code: 'SessionTagLib.fullName.profile')
        }
    }

    def language = {attrs, body ->
        out << "<select>"
        out << "<option value=\"en\">English</value>"
        out << "<option value=\"es\">Espa&#241;ol</value>"
        out << "<option value=\"fr\">Fran&#231;ais</value>"
        out << "</select>"
    }
}